package com.lzx.applib.data;

import android.content.Context;
import android.util.Xml;

import com.lzx.applib.utils.StreamUtil;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lzx on 16/09/12 0012.
 */
public class AppUrlManager {

    private static final String TAG = "AppUrlManager";

    private static final String URL_PATH = "url.xml";
    private static Map<String, Class<? extends  IXmlParser>> parserMap;


    private AppUrlManager() {}

    static {
        parserMap = new HashMap<>();
        parserMap.put("app", AppUrls.class);
        parserMap.put("url", AppUrl.class);
    }

    public static final AppUrls init(Context context) {
        InputStream in = null;
        try {
            in = context.getAssets().open(URL_PATH);
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(in, "utf-8");

            parser.nextTag();
            AppUrls appUrls = new AppUrls();
            appUrls.parse(parser);

            return appUrls;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } finally {
            StreamUtil.closeSafely(in);
        }
        return null;
    }

}
